function add_main_class() {
    var obj = document.getElementById('min-class');
    html = obj.cloneNode(true);
    html.id = '';
    html.getElementsByTagName('button')[0].onclick = ad_class;
    clas = document.getElementById('class');
    var li = clas.getElementsByTagName('li')[0] || null;
    clas.insertBefore(html, li);
};
//添加子类
function ad_class(obj) {
	if(obj.innerHTML){// 判断是否为dom对象
		var clas_li = obj.parentNode;
	}else{

		var clas_li = this.parentNode;
	}
    
    var ul = clas_li.getElementsByTagName('ul')[0],
        obj = document.getElementById('m-class'),
        li = ul.getElementsByTagName('li')[0];
    var span = clas_li.getElementsByTagName('span')[0];
    span.style.display = 'none';
    html = obj.cloneNode(true);
    html.id = '';
    ul.insertBefore(html, li);
}

//删除主类
function remove_min_class(obj) {
    if (obj && obj.parentNode && obj.parentNode.parentNode) {
        var o = obj.parentNode.parentNode;
        var name = o.getElementsByTagName('input')[0].value || '这个';
        var ok = confirm('你确定要删除' + name + '主类吗');
        if (ok) {
            obj.parentNode.parentNode.removeChild(obj.parentNode)
        };
    };
}

//删除子类
function remove_class(obj) {
    if (obj && obj.parentNode && obj.parentNode.parentNode) {
        var ul = obj.parentNode.parentNode;
        var ok = confirm('你确定要删除这个子类吗');
        if (ok) {
            obj.parentNode.parentNode.removeChild(obj.parentNode)
        }
        if (!ul.getElementsByTagName('li').length) {
            ul.parentNode.getElementsByTagName('span')[0].style.display = 'inline';
        };
    };
};


function class_submits() {
    var clas = document.getElementById('class'),
        li = clas.childNodes,
        data = [],
        ul = '',
        z_li = '',
    	name = '',
    	goodsid=document.getElementById('goodsid').value;
    for (var i = 0, l = li.length; i < l; i++) {
    	if(li[i].nodeName.toLowerCase() == 'li'){
    		console.log(li[i].getElementsByTagName('ul').length)
    		ul = li[i].getElementsByTagName('ul')[0];
	        z_li = ul.getElementsByTagName('li');
	        name = li[i].getElementsByTagName('input')[0].value;
	        if (z_li.length) {
	            var obj = {
	                name: name,
	                classs: [],
	                goodsid:goodsid
	            }
	            var input = '';
	            for (var j = 0, k = z_li.length; j < k; j++) {
	                input = z_li[j].getElementsByTagName('input');
	                var o = {
	                    name: input[0].value,
	                    num: input[2].value,
	                    price: input[1].value
	                }
	                obj.classs.push(o);
	            }
	            data.push(obj)
	        } else {
	            var span = li[i].getElementsByTagName('span')[0],
	                input = span.getElementsByTagName('input');
	            var obj = {
	                name: name,
	                num: input[1].value,
	                price: input[0].value,
	                goodsid:goodsid
	            }
	            data.push(obj);
	        }
    	}
    }
    var data = JSON.stringify(data);
    var url = 'addgoods.php?data='+data;
    $.get(url,function(data){
    	console.log(data);
    })
}

function guige_submits() {
    var clas = document.getElementById('class'),
        li = clas.childNodes,
        data = [],
        ul = '',
        z_li = '',
        name = '',
        ziid=document.getElementById('ziid').value;
        id=document.getElementById('id').value;
    for (var i = 0, l = li.length; i < l; i++) {
        if(li[i].nodeName.toLowerCase() == 'li'){
            console.log(li[i].getElementsByTagName('ul').length)
            ul = li[i].getElementsByTagName('ul')[0];
            z_li = ul.getElementsByTagName('li');
            name = li[i].getElementsByTagName('input')[0].value;
            if (z_li.length) {
                var obj = {
                    name: name,
                    classs: [],
                    ziid:ziid,
                    id:id
                }
                var input = '';
                for (var j = 0, k = z_li.length; j < k; j++) {
                    input = z_li[j].getElementsByTagName('input');
                    var o = {
                        name: input[0].value,
                        num: input[2].value,
                        price: input[1].value
                    }
                    obj.classs.push(o);
                }
                data.push(obj)
            } else {
                var span = li[i].getElementsByTagName('span')[0],
                    input = span.getElementsByTagName('input');
                var obj = {
                    name: name,
                    num: input[1].value,
                    price: input[0].value,
                    ziid:ziid,
                    id:id
                }
                data.push(obj);
            }
        }
    }
    var data = JSON.stringify(data);

    var url = 'addguige.php?data='+data;

    $.get(url,function(data){
        alert('添加成功');
        window.history.back(-1);
        console.log(data);
    })
}